package com.lideru.backstage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideru.backstage.entity.Book;
import com.lideru.backstage.entity.BookLabel;
import com.lideru.backstage.entity.BookLabelRecord;
import com.lideru.backstage.handler.NoAuth;
import com.lideru.backstage.mapper.BookLabelMapper;
import com.lideru.backstage.mapper.BookLabelRecordMapper;
import com.lideru.backstage.mapper.BookMapper;
import com.lideru.backstage.utils.JWTUtils;
import com.lideru.backstage.utils.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * @author lideru
 */
@RestController
@RequestMapping("/book")

public class BookController {
    @Resource
    private BookMapper bookMapper;
    @Resource
    private BookLabelRecordMapper bookLabelRecordMapper;
    @Resource
    private BookLabelMapper bookLabelMapper;

    @PostMapping
    public Result<?> save(@RequestBody Book book, HttpServletRequest request) {
        book.setGmtCreate(new Date());
        book.setGmtModified(new Date());
        book.setUserId(JWTUtils.getUserId(request.getHeader("token")));
        int i = bookMapper.insert(book);
        if (i<1){
            return Result.error("-1","新增书本失败！");
        }
        return Result.success();
    }
    @GetMapping("/getBookById/{id}")
    @NoAuth
    public Result<?> getBookById(@PathVariable Long id) {
        Book book = bookMapper.selectOne(Wrappers.<Book>lambdaQuery().eq(Book::getId,id));
        if (book==null){
            return Result.error("-1","没有信息！");
        }
        return Result.success(book);
    }


    @PutMapping
    public Result<?> update(@RequestBody Book book) {
        book.setGmtModified(new Date());
        int i = bookMapper.updateById(book);
        if (i<1){
            return Result.error("-1","修改书本失败！");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id, HttpServletRequest request) {
        Book book = bookMapper.selectOne(Wrappers.<Book>lambdaQuery().
                eq(Book::getUserId, JWTUtils.getUserId(request.getHeader("token")))
                .eq(Book::getId, id));
        if (book != null) {
            bookMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("403", "你没有权限");

    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,  HttpServletRequest request) {
        Page<Book> pages = bookMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<Book>lambdaQuery().
                like(Book::getName, search).eq(Book::getUserId, JWTUtils.getUserId(request.getHeader("token"))));
        return Result.success(pages);
    }
    @GetMapping("/search")
    @NoAuth
    public Result<?> findSearchPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "36") Integer pageSize,
                              @RequestParam(defaultValue = "") String search ) {
        Page<Book> pages = bookMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<Book>lambdaQuery().like(Book::getName, search));
        return Result.success(pages);
    }
    @GetMapping("getUserAllBook")
    @NoAuth
    public Result<?> findPage(  HttpServletRequest request) {
        Long userId ;
        try {
             userId = JWTUtils.getUserId(request.getHeader("token"));

        }catch (Exception e){
            return (Result<?>) Result.NOTLOGIN("登录过期","/login");
        }
        List<Book> books = bookMapper.selectList(Wrappers.<Book>lambdaQuery().eq(Book::getUserId,userId));
        if (books.size()>0){
            return Result.success(books);
        }
        return Result.error("-1","系统错误");
    }

    @GetMapping("/getBook")
    @NoAuth
    public Result<?> getBook() {
        List<Book> recommendBookList = bookMapper.selectList(Wrappers.<Book>lambdaQuery().eq(Book::getRecommend, 1).last("limit 10"));
        List<Book> righteousArdourList =  bookMapper.selectList(Wrappers.<Book>lambdaQuery().eq(Book::getBookType, "热血").last("limit 10"));
        List<Book> fantasyList =  bookMapper.selectList(Wrappers.<Book>lambdaQuery().eq(Book::getBookType, "玄幻").last("limit 10"));
        List<Book> boyList =  bookMapper.selectList(Wrappers.<Book>lambdaQuery().eq(Book::getBookType, "男生").last("limit 10"));
        List<Book> girlList =  bookMapper.selectList(Wrappers.<Book>lambdaQuery().eq(Book::getBookType, "女生").last("limit 10"));
        List<Book> rankList =  bookMapper.selectList(Wrappers.<Book>lambdaQuery().eq(Book::getRecommend, 2).last("limit 10"));
        HashMap map = new HashMap(5);
        map.put("recommendBookList",recommendBookList);
        map.put("righteousArdourList",righteousArdourList);
        map.put("fantasyList",fantasyList);
        map.put("boyList",boyList);
        map.put("girlList",girlList);
        map.put("rankList",rankList);
        return Result.success(map);
    }

    @GetMapping("/getBookRanking")
    @NoAuth
    public Result<?>  getBookRanking(){
        List<Book> historyBook = bookMapper.countBookByHistory();
        List<Book> bookrackBook = bookMapper.countBookByBookrack();
        List<Book> favoriteBook = bookMapper.countBookByFavorite();
        List<Book> messageBook = bookMapper.countBookByMessage();
        List<Book> newBook = bookMapper.countBookByNew();
        HashMap map = new HashMap();
        map.put("historyBook",historyBook);
        map.put("bookrackBook",bookrackBook);
        map.put("favoriteBook",favoriteBook);
        map.put("messageBook",messageBook);
        map.put("newBook",newBook);
        return Result.success(map);
    }



}
